java generate XML method [with demo source download]

  • 2020-05-26 08:23:18
  • OfStack

The example in this article shows how to generate XML from java. I will share it with you for your reference as follows:

The drop-down box was generated by javascript reading the xml file. The Xml file is generated from the database. The Xml file is just one cache from the page to the database. This is good for performance. Generating the xml file is another chore. The machine had to do it. The real scenario is when the program automatically or manually triggers the program to generate xml on a regular basis. Today, I wrote a small program to separate out the function generated by xml file.

The implementation is to read the SQL statement using jxl.jar (I'll admit I love writing configuration using Execel). SQL specifies what is the name, what is the code, and what is the parent code. Mybatis queries data, assembles messages and writes them to files. This time I wrote an jar package. Please prepare jre before running.

Core code: XmlCreateService.java


package com.fitweber.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.fitweber.util.CommonUtils;
import com.fitweber.util.ExecelUtils;
/**
 * <pre>
 * XML File generator 
 * </pre>
 * @author wheatmark hajima11@163.com
 * @version 1.00.00
 * <pre>
 *  Modify the record 
 *  Modified version :   The modifier:   Modification date :   Modify the content :
 * </pre>
 */
public class XmlCreateService {
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
public static void main(String[] argc){
String resource = "META-INF/conf/mybatis-config.xml";
String root = "";
InputStream inputStream;
try {
// Get the database connection 
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
// Get the query parameters 
List requestList = ExecelUtils.readExecelSimple("xmlmaker.xls");
// Define variables 
int i,j,listSize;
String filename,sqlstament,temp;;
HashMap requestMap = new HashMap();
Map map;
StringBuffer buf = new StringBuffer();
for(Object l:requestList){
List list = (List)l;
listSize = list.size();
filename =(String)list.get(1);
sqlstament =(String)list.get(2);
requestMap.put("sql", sqlstament);
List result = session.selectList("com.fitweber.dao.XmlCreateDao.xmlDataQuery",requestMap);
for(Object r:result){
buf.append("<option>");
map=(Map)r;
temp = (String) map.get("DM");
if(temp!=null){
buf.append("<dm>"+temp+"</dm>");
}
temp = (String) map.get("MC");
if(temp!=null){
buf.append("<mc>"+temp+"</mc>");
}
temp = (String) map.get("PC");
if(temp!=null){
buf.append("<pc>"+temp+"</pc>");
}
temp = (String) map.get("ITEM");
if(temp!=null){
buf.append("<item>"+temp+"</item>");
}
buf.append("</option>");
}
CommonUtils.saveFile(null, (System.getProperty("user.dir")+"\\xml\\").replace("\\", "/")+filename, ("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><select>"+buf.toString()+"</select></root>"),false);
buf.setLength(0);
}
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Complete source code in github maintenance, address: https: / / github com/ladykiller/xmlmaker.

Click here to download the complete example code.

PS: here are a few more online tools for you to use about xml operations:

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

Online formatting XML/ online compression XML:
http://tools.ofstack.com/code/xmlformat

XML online compression/formatting tool:
http://tools.ofstack.com/code/xml_format_compress

XML code online formatting beautification tool:
http://tools.ofstack.com/code/xmlcodeformat

I hope this article is helpful for you to design java program.


Related articles: